package com.network.topo.netwatch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.network.util.FilePathUtil;

public class ArpTableFetcher {

    private String ifDescrOID = ".1.3.6.1.2.1.2.2.1.2";
    private String arpTableOID = ".1.3.6.1.2.1.4.22.1.2";
    
    public String host;
    public String community;
    
    private String snmpwalkCmdArpPrefix ="snmpwalk -v 1  -c ";
    private String snmpwalkCmdIntPrefix = "snmpwalk -v 1  -c ";
    private String snmpwalkCmdArpTable = null;
    private String snmpwalkCmdIntTable = null;
    
    public ArpTableFetcher(String hostName,String commStr) {
        host = hostName;
        community = commStr;
        snmpwalkCmdArpTable =FilePathUtil.getRootPath() + "snmp/" + snmpwalkCmdArpPrefix + community + " " + host+" "+arpTableOID;
        snmpwalkCmdIntTable = FilePathUtil.getRootPath() + "snmp/" + snmpwalkCmdIntPrefix + community + " " + host+" "+ifDescrOID;
    }
    
    private String[] parseArpEntry(String str) {
//        Pattern pattern = Pattern.compile(
//                "//.1//.3//.6//.1//.2//.1//.4//.22//.1//.2//.(//d+)//.(.+?)//s(.+)");
//        Matcher matcher = pattern.matcher(str);
//        if (matcher.matches()) {
//            String[] result = {matcher.group(1),matcher.group(2),matcher.group(3)};
//            return result;
//        }
    	String[] result =  new String[3];
    	if( str == null ){
    		return null;
    	}
    	String strLast = str.replace("iso.3.6.1.2.1.4.22.1.2.", "");
    	String index = strLast.substring(0,strLast.indexOf("."));
    	String ip = strLast.substring(strLast.indexOf(".") + 1,strLast.indexOf("=") -1);
    	String mac =strLast.substring(strLast.indexOf(":") + 1,strLast.length());
    	result[0] = index.trim();
    	result[1] = ip.trim();
    	result[2] = mac.trim();
    	return result;
    }
    public void g(){
    	String str = "iso.3.6.1.2.1.4.22.1.2.1.127.0.0.21 = Hex-STRING: 00 00 12 00 00 00"; 
    	
    }
    
    private String[] parseIntEntry(String str) {
        Pattern pattern = Pattern.compile(
                "//.1//.3//.6//.1//.2//.1//.2//.2//.1//.2//.(//d+)//s(.+)");
        Matcher matcher = pattern.matcher(str);
        if (matcher.matches()) {
            String[] result = {matcher.group(1),matcher.group(2)};
            return result;
        }
        return null;
    }
    
    private String getIntName(ArrayList itTable,String idx) {
        for (int i=0;i<itTable.size();i++) {
            String[] entry = parseIntEntry(itTable.get(i).toString());
            if (entry[0].equals(idx)) return entry[1];
        }
        return null;
    }
    
    public ArrayList getArpTable() {
        ArrayList arpList = getSnmpwalkOutput(snmpwalkCmdArpTable);
        ArrayList intList = getSnmpwalkOutput(snmpwalkCmdIntTable);
        ArrayList arpTable = new ArrayList();
        for (int i=0; i<arpList.size(); i++) {
            String[] entry = parseArpEntry(arpList.get(i).toString());
            entry[0] = getIntName(intList,entry[0].toString());
            arpTable.add(entry);
        }
        arpList = null;
        intList = null;
        return arpTable;
    }
    
    private ArrayList getSnmpwalkOutput(String cmd) {
        ArrayList list = new ArrayList();
        try {
            String outString;
            Process process = Runtime.getRuntime().exec(cmd);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((outString = reader.readLine()) != null) {
                list.add(outString);
            }
            return list;
        }
        catch (IOException e) {System.err.println(e.getMessage());}
        return null;
    }
}